package cn.shyouchi.wuguanjia.android.ui.login;

import android.content.Intent;
import android.text.InputType;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.model.LoginMod;
import cn.shyouchi.wuguanjia.android.ui.home.property.PropertyMainActivity;
import cn.shyouchi.wuguanjia.android.ui.home.security.SecurityMainActivity;
import cn.shyouchi.wuguanjia.android.ui.register.RegisterActivity;

/**
 * A login screen that offers login via email/password.
 */
public class LoginActivity extends BaseActivity implements LoginContract.View {

    @BindView(R.id.login_telephone_edit_text)
    EditText telephone;
    @BindView(R.id.login_password_edit_text)
    EditText password;
    @BindView(R.id.user_login_see_password_icon)
    ImageView togglePasswordIcon;
    @BindView(R.id.user_login_remember_password_icon)
    ImageView rememberPasswordIcon;
    private static final String TAG = "LoginActivity";


    private boolean isShowPassword = false, isChecked = false;


    LoginPresenter mPresenter;

    @Override
    public int initContentView() {
        return R.layout.activity_login;
    }

    @Override
    public void initInjector() {

        mPresenter = new LoginPresenter(this);
        mPresenter.attachView(this);

    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @OnClick(R.id.login_submit_button)
    void loginSubmitButtonTapped() {
        if (telephone.getText().toString().isEmpty()) {
            Toast.makeText(this, getText(R.string.please_type_telephone), Toast.LENGTH_SHORT).show();
            return;
        }
        if (password.getText().toString().isEmpty()) {
            Toast.makeText(this, getText(R.string.please_type_password), Toast.LENGTH_SHORT).show();
            return;
        }
        mPresenter.login(telephone.getText().toString(),password.getText().toString());
    }


    @OnClick(R.id.user_login_see_password_icon)
    void togglePasswordIconTapped() {
        isShowPassword = !isShowPassword;
        int resourceId = isShowPassword ? R.mipmap.ic_eye_close : R.mipmap.ic_eye_open;
        int inputType = isShowPassword ? InputType.TYPE_TEXT_VARIATION_PASSWORD : InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD;
        togglePasswordIcon.setImageResource(resourceId);
        password.setInputType(inputType);
    }

    @OnClick(R.id.user_login_remember_password_icon)
    void toggleRememberPasswordIconTapped() {
        isChecked = !isChecked;
        int resourceId = isChecked ? R.mipmap.ic_check_yes : R.mipmap.ic_remember_password;
        rememberPasswordIcon.setImageResource(resourceId);
    }

    @Override
    public void jumpMain(LoginMod mod) {
        if (mod.getData().getType() == 1) {
            startActivity(new Intent(LoginActivity.this, SecurityMainActivity.class));
            finish();
        } else if (mod.getData().getType()  == 2) {
            startActivity(new Intent(LoginActivity.this, PropertyMainActivity.class));
            finish();
        }
    }

    @OnClick(R.id.login_redirect_to_register_button)
    @Override
    public void jumpRegister() {
        startActivity(new Intent(this, RegisterActivity.class));
    }
}

